package com.ruoyi.mng.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Supplier;

import com.ruoyi.mng.domain.*;
import com.ruoyi.mng.service.IHzBusiService;
import com.ruoyi.mng.service.IHzChaChannelService;
import com.ruoyi.mng.service.IHzMerMerchantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mng.service.IHzOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 订单管理Controller
 * 
 * @author chenqiang
 * @date 2023-04-29
 */
@Controller
@RequestMapping("/mng/order")
public class HzOrderController extends BaseController
{
    private String prefix = "mng/order";

    @Autowired
    private IHzOrderService hzOrderService;
    @Resource
    private IHzMerMerchantService hzMerMerchantService;
    @Resource
    private IHzBusiService hzBusiService;
    @Resource
    private IHzChaChannelService hzChaChannelService;

    @RequiresPermissions("mng:order:view")
    @GetMapping()
    public String order( ModelMap mmap)
    {
        Supplier<HzMerMerchant> merchantSupplier = HzMerMerchant::new;
        List<HzMerMerchant> hzMerMerchantList = hzMerMerchantService.selectHzMerMerchantList(merchantSupplier.get());
        ArrayList<HashMap<String, String>> merchantList = new ArrayList<>();
        hzMerMerchantList.forEach(merMerchant -> {
            HashMap<String, String> merchantMap = new HashMap<>();
            merchantMap.put("merchantId", merMerchant.getMerchantId());
            merchantMap.put("merchantName", merMerchant.getMerchantShortName());
            merchantList.add(merchantMap);
        });

        Supplier<HzBusi> busiSupplier = HzBusi::new;
        List<HzBusi> hzBusiList = hzBusiService.selectHzBusiList(busiSupplier.get());
        ArrayList<HashMap<String, String>> busiList = new ArrayList<>();
        hzBusiList.forEach(busi -> {
            HashMap<String, String> busiMap = new HashMap<>();
            busiMap.put("id", busi.getBusiId().toString());
            busiMap.put("name", busi.getBusiName());
            busiList.add(busiMap);
        });

        Supplier<HzChaChannel> channelSupplier = HzChaChannel::new;
        List<HzChaChannel> hzChaChannelList = hzChaChannelService.selectHzChaChannelList(channelSupplier.get());
        ArrayList<HashMap<String, Object>> channelList = new ArrayList<>();
        hzChaChannelList.forEach(chaChannel -> {
            HashMap<String, Object> channelMap = new HashMap<>();
            channelMap.put("channelId", chaChannel.getChannelId());
            channelMap.put("channelName", chaChannel.getChannelName());
            channelList.add(channelMap);
        });


        mmap.put("busiList", busiList);
        mmap.put("merchantList", merchantList);
        mmap.put("channelList", channelList);
        return prefix + "/order";
    }

    /**
     * 查询订单管理列表
     */
    @RequiresPermissions("mng:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HzOrder hzOrder)
    {
        startPage();
        List<HzOrder> list = hzOrderService.selectHzOrderList(hzOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @RequiresPermissions("mng:order:export")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HzOrder hzOrder)
    {
        List<HzOrder> list = hzOrderService.selectHzOrderList(hzOrder);
        ExcelUtil<HzOrder> util = new ExcelUtil<HzOrder>(HzOrder.class);
        return util.exportExcel(list, "订单管理数据");
    }

    /**
     * 新增订单管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单管理
     */
    @RequiresPermissions("mng:order:add")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HzOrder hzOrder)
    {
        return toAjax(hzOrderService.insertHzOrder(hzOrder));
    }

    /**
     * 修改订单管理
     */
    @RequiresPermissions("mng:order:edit")
    @GetMapping("/edit/{orderId}")
    public String edit(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        HzOrder hzOrder = hzOrderService.selectHzOrderByOrderId(orderId);
        mmap.put("hzOrder", hzOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单管理
     */
    @RequiresPermissions("mng:order:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HzOrder hzOrder)
    {
        return toAjax(hzOrderService.updateHzOrder(hzOrder));
    }

    /**
     * 删除订单管理
     */
    @RequiresPermissions("mng:order:remove")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(hzOrderService.deleteHzOrderByOrderIds(ids));
    }

    /**
     * 查看订单
     */
    @RequiresPermissions("mng:order:list")
    @GetMapping("/detail/{orderId}")
    public String detail(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        HzOrder hzOrder = hzOrderService.selectHzOrderByOrderId(orderId);
        mmap.put("hzOrder", hzOrder);
        return prefix + "/detail";
    }
}
